package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.odin.solution.contract.oms.UpdateUserSafeReq;
import com.homedo.odin.solution.contract.oms.UserSafeReq;
import com.homedo.odin.solution.model.request.mng.MngUserSafeImportReq;
import com.homedo.odin.solution.model.request.mng.MngUserSafeReqsListRequestBean;
import com.homedo.odin.solution.model.response.mng.MngBatchInputResponse;
import com.homedo.odin.solution.model.response.mng.MngUserSafePageResponseBean;
import com.homedo.odin.solution.service.UserSafeServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liyi on 2018/5/22. mng 白名单
 */
@Api(description = "白名单", tags = {"UserSafe"}, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/user_info")
public class MngUserSafeController {

    @Autowired
    private UserSafeServiceImpl baseService;


    @ApiOperation(value = "Mng_批量导入白名单", httpMethod = "POST", response = MngBatchInputResponse.class)
    @PostMapping(value = "/safes/{bizCode}")
    public ResultData batchInput(@ApiParam(value = "所属业务类型(需求PRODUCT)", required = true) @PathVariable String bizCode,
            @RequestBody MngUserSafeImportReq requestBean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(new Meta(Meta.CODE_451.getCode(), defaultMessage));
        }
        MngBatchInputResponse responseBean = baseService.batchInput(requestBean, bizCode);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }

    @ApiOperation(value = "Mng_分页列表查询", httpMethod = "GET", response = MngUserSafePageResponseBean.class)
    @GetMapping(value = "/safes/{bizCode}")
    public ResultData userSafeList(@ApiParam(value = "所属业务类型(需求PRODUCT)", required = true) @PathVariable String bizCode,
            @Valid @ModelAttribute MngUserSafeReqsListRequestBean requestBean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        MngUserSafePageResponseBean responseBean = baseService.userSafeList(requestBean, bizCode);
        return ResultData.newInstance(Meta.CODE_200, responseBean);
    }


    @ApiOperation(value = "Mng_白名单审核", httpMethod = "POST")
    @PostMapping(value = "/safes/{bizCode}/{status}")
    public ResultData check(@Valid @RequestBody UserSafeReq requestBean,
            @ApiParam(value = "所属业务类型(需求PRODUCT)", required = true) @PathVariable String bizCode,
            @ApiParam(value = "状态 0：待审核，1：审核通过，-1：审核不通过", required = true) @PathVariable Integer status,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        baseService.check(requestBean, bizCode, status);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @ApiOperation(value = "Mng_白名单批量删除", httpMethod = "DELETE")
    @DeleteMapping(value = "/safes/{bizCode}")
    public ResultData batchDelete(@ApiParam(value = "所属业务类型(需求PRODUCT)", required = true) @PathVariable String bizCode,
            @RequestBody UpdateUserSafeReq req) {
        baseService.deleteUserSafe(req.getIds(), bizCode);
        return ResultData.newInstance(Meta.CODE_200, null);
    }
}
